package team.se.tms.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import team.se.tms.dto.MenuDetail;

import java.util.ArrayList;
import java.util.List;

/**
 * 权限管理辅助工具类
 *
 * @author winkyle
 * @date 2018/11/21
 */
@Slf4j
public class Jurisdiction {

    /**
     * 获取shiro管理的session
     *
     * @return Session
     * @create on 2018/11/21 by winkyle
     */
    public static Session getSession() {
        return SecurityUtils.getSubject().getSession();
    }

    /**
     * 获取有权访问的菜单
     *
     * @param allMenuDetails 全部菜单列表
     * @return 有权访问的菜单列表
     * @create on 2018/12/15 by winkyle
     */
    public static List<MenuDetail> getAllowableMenus(List<MenuDetail> allMenuDetails){
        List<MenuDetail> menuDetails = new ArrayList<>();
        for(MenuDetail menuDetail:allMenuDetails){
            if(menuDetail!=null&&SecurityUtils.getSubject().isPermitted(menuDetail.getAuthorityId())){
                menuDetails.add(menuDetail);
            }
        }
        return menuDetails;
    }
}
